package gamagora.game.animations
{
	import gamagora.game.utils.OSImages;
	
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	
	public class OSMonkey extends OSAnimation
	{
		private static const IDLE:String = "idle";
		private static const START_RUN:String = "start run";
		private static const STOP_RUN:String = "stop run";
		private static const JUMP:String = "jump";
		private static const JUMP_ON_SIDE:String = "jump on side"; 
		private static const RUN:String = "run";
		
		private static const SPEED:Number = 40.0;

		private var _move:Boolean;
		private var _moveToX:Number;
		
		public function OSMonkey(X:Number=0, Y:Number=0, background:uint = OSAnimation.NO_BACKGROUND)
		{
			super(X, Y, background);
			
			if(background != OSAnimation.NO_BACKGROUND)
			{
				loadGraphic(OSImages.MONKEY2, true, true, 65, 65);
				offset.y = 27;
			}
			else 
			{
				//TODO ADD NORMAL MONKEY + OFFSET
			}
			
			addAnimation(IDLE, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 15);
			addAnimation(START_RUN, [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38], 20, false);
			addAnimation(STOP_RUN, [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58], 20, false);
			addAnimation(JUMP, [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84], 10, false);
			addAnimation(JUMP_ON_SIDE, [85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104], 10, false);
			addAnimation(RUN, [105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125], 15);
			
			play(IDLE);
		}
		
		public override function update():void
		{
			super.update();
			if(finished)
			{
				switch(_curAnim.name)
				{
					case START_RUN:
						play(RUN);
						break;
					case STOP_RUN:
						play(IDLE);
						break;
				}
			}
			
			if(_move && (_curAnim.name == RUN ||_curAnim.name == JUMP_ON_SIDE))
			{
				if(x < _moveToX)
				{
					x += FlxG.elapsed * SPEED;	
					if(x >= _moveToX)
					{
						x = _moveToX;
						play(STOP_RUN);
						_move = false;
					}
				}
				else 
				{
					x -= FlxG.elapsed * SPEED;
					if(x <= _moveToX)
					{
						x = _moveToX;
						play(STOP_RUN);
						_move = false;
					}
				}
			}
		}
		
		public function moveTo(newX:Number):void
		{
			_move = true;
			_moveToX = newX;
			_facing = x < _moveToX ? LEFT : RIGHT;
			play(START_RUN, true);
		}

		public function get move():Boolean
		{
			return _move;
		}

	}
}
